using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Common;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ContentNoteList : AdminBaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }

        }

        protected void gvNode_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            string name = e.CommandArgument.ToString();


            IGlobalist.Cms.Model.ContentNoteList contentNoteList = new IGlobalist.Cms.Model.ContentNoteList();

            if (ViewState["contentNoteList"] != null) contentNoteList = (IGlobalist.Cms.Model.ContentNoteList)ViewState["contentNoteList"];

            if (e.CommandName == "delNode")
            {
                foreach (ContentNote cn in contentNoteList)
                {
                    if ((name == cn.Name) && (cn.ActionType != ActionType.Delete))
                    {
                        ContentNote cNote = cn;
                        //set actiontype
                        contentNoteList.Remove(cNote);
                        if (cNote.ActionType != ActionType.Add) 
                        {
                            cNote.ActionType = ActionType.Delete;
                            contentNoteList.Add(cNote);
                        }
                        
                        BindNode(contentNoteList);
                        ViewState["contentNoteList"] = contentNoteList;
                        break;
                    }
                }
            }

            if (e.CommandName == "editNode")
            {
                foreach (ContentNote cn in contentNoteList)
                {
                    if (name == cn.Name)
                    {
                        ViewState["name"] = name;
                        tbDescription.Text = cn.Description;
                        tbName.Text = cn.Name;
                        tbName.Enabled = false;
                        break;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IGlobalist.Cms.Model.ContentNoteList contentNoteList = new IGlobalist.Cms.Model.ContentNoteList();
                if (ViewState["contentNoteList"] != null) contentNoteList = (IGlobalist.Cms.Model.ContentNoteList)ViewState["contentNoteList"];
                ContentNote contentNote = new ContentNote();
                ContentNote cNote = new ContentNote();
                contentNote.Name = tbName.Text.Replace(" ", "");
                if (IsExist(contentNote.Name))
                {
                    if (ViewState["name"] != null)
                    {
                        string name = Convert.ToString(ViewState["name"]);
                        foreach (ContentNote cn in contentNoteList)
                        {
                            if ((name == cn.Name) && (cn.ActionType != ActionType.Delete))
                            {
                                cNote = cn;
                            }
                        }

                        if (cNote.ActionType == ActionType.None)
                            contentNote.ActionType = ActionType.Edit;
                        contentNoteList.Remove(cNote);
                        ViewState["name"] = null;

                    }
                    else
                    {
                        contentNote.ActionType = ActionType.Add;
                    }
                   
                    IList<ICmsClass> contentNodeClass = ClassController.GetClassListByClassTypeID(Convert.ToInt16(CmsObjectType.ContentNote));
                    ICmsClass clazz = ClassController.GetClassByUsageType(Convert.ToInt16(CmsObjectType.ContentNote), contentNodeClass[0].UsageType);
                    contentNote.Class = clazz;
                    contentNote.ClassID = clazz.ID;
                    contentNote.ClassTypeID = Convert.ToInt16(CmsObjectType.ContentNote);
                    contentNote.Name = tbName.Text;
                    contentNote.Description = tbDescription.Text;
                    contentNoteList.Add(contentNote);
                    ViewState["contentNoteList"] = contentNoteList;
                    //referst list
                    BindNode(contentNoteList);
                    tbDescription.Text = "";
                    tbName.Text = "";
                    tbName.Enabled = true;
                    lblError.Text = "";
                }
                else
                {
                    lblError.Text = "Name is exist";
                }
            }
            catch (Exception ex)
            {
                Logger.Debug("[[Error]]", ex);
            }

        }

        public void BindNode(IGlobalist.Cms.Model.ContentNoteList contentNoteList)
        {
            ViewState["contentNoteList"] = contentNoteList;
            IGlobalist.Cms.Model.ContentNoteList listNodeResult = new IGlobalist.Cms.Model.ContentNoteList();
            foreach (ContentNote cn in contentNoteList)
            {
                if (cn.ActionType != ActionType.Delete) listNodeResult.Add(cn);
            }

                gvNode.DataSource = listNodeResult;
                gvNode.DataBind();

        }

        public IGlobalist.Cms.Model.ContentNoteList NoteList
        {
            get
            {
                return (IGlobalist.Cms.Model.ContentNoteList)ViewState["contentNoteList"];
            }
        }

        public bool IsExist(string name)
        {
            IGlobalist.Cms.Model.ContentNoteList contentNoteList =new IGlobalist.Cms.Model.ContentNoteList();
            if (ViewState["contentNoteList"] != null) contentNoteList = (IGlobalist.Cms.Model.ContentNoteList)ViewState["contentNoteList"];
            if (ViewState["name"] == null)
            {
                foreach (ContentNote cn in contentNoteList)
                {
                    if ((name == cn.Name) && (cn.ActionType != ActionType.Delete)) return false;
                }
            }
            return true;
        }
    }
}